﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Com.CMS263.Util;
using Com.CMS263.PO;

namespace Com.CMS263.BLL
{
   public  class TransformList
    {
        /// <summary>
        /// 要解析的含有标签的html代码
        /// </summary>
        private string ctx = null;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="ctx">页面内容</param>
        /// 
        List<string> tags = new List<string>();


        TagUtil tu = new TagUtil();

        ArtBaseBLL bll = new ArtBaseBLL();
        ExtValueBLL evbll = new ExtValueBLL();
        public TransformList(string ctx)
        {
            this.ctx = ctx;
        }
        /// <summary>
        /// 得到替换标签后的内容
        /// </summary>
        /// <returns>返回替换后的结果</returns>
        public string GetResulte()
        {
            //得到所有标签
            tags = tu.GetTag(ctx, "cms:list", tags);
            foreach (string tag in tags)
            {
                /// <summary>
                /// 文章分类编号
                /// </summary>
                int menuID = 0;
                /// <summary>
                /// 每页文章条数，默认为10条
                /// </summary>
                int pageSize = 10;
                /// <summary>
                /// 取得编号的request参数的名字
                /// </summary>
                string paraName = "";
                //得到包含属性的标签字符串
                int end = tag.IndexOf(">");
                string arrtStr = tag.Substring(1, end - 1);
                //标签呗替换后生成的html
                string tagInnerHTML = "";
                //解析标签得到所有的属性
                
                Dictionary<string, string> attrDic = tu.Getattribute(arrtStr);
                if (!string.IsNullOrEmpty(attrDic["menuid"]))
                {
                    if (attrDic["menuid"].ToLower() != "\"request\"")
                    {
                        string str = attrDic["menuid"].Replace("\"", "");
                        menuID = int.Parse(str);
                    }
                    else
                    {
                        //如果是通过传递过来的参数取分类的那么就用参数得到编号
                        string para = System.Web.HttpContext.Current.Request[attrDic["paraname"].Replace("\"", "")];
                        if (!string.IsNullOrEmpty(para))
                        {
                            menuID = int.Parse(para);
                        }
                        else
                        {
                            throw new Exception("分类参数名称错误，请使用正确的参数名称传递参数");
                        }
                    }
                }
                else
                {
                    throw new Exception("标签解析错误,请填写正确的分类标签");
                }


                if (!string.IsNullOrEmpty(attrDic["pagesize"]))
                {
                    pageSize = int.Parse(attrDic["pagesize"].Replace("\"", ""));
                }

                //根据上面的到的编号和页码数取得所要的数据
              
                IList<ArtBase> ablist = new List<ArtBase>();
               // ablist = bll.GetListBySortId(menuID, "ASC", "", 1, pageSize);
                ArtBase ab1 = new ArtBase();
                ab1.addDate = DateTime.Now;
                ab1.title = "测试一";
                ab1.id = 1;
                ab1.menuId = 20;


                ArtBase ab2 = new ArtBase();
                ab2.addDate = DateTime.Now;
                ab2.title = "测试二";
                ab2.id = 2;
                ab2.menuId = 20;
                ablist.Add(ab1);
                ablist.Add(ab2);
                foreach (ArtBase a in ablist) 
                {
                    a.valueList = evbll.GetExtValueByArtId(a.id);
                }
                //解析列表标签，循环替换出列表
                List<string> links = new List<string>();
                links = tu.GetTag(tag, "cms:links", links);
                //循环连接连接标签

                foreach (string str in links)
                {
                    string innerHTML = tu.GetInnerHTML("cms:links",str);
                    //解析里面的<cms:attribute  field=""  length=""/>标签并用一条数据中的各个属性替换标签
                    List<string> attrs = new List<string>();
                    attrs = tu.GetTag(str, "cms:attribute", attrs);
                    //循环分析连接中的属性标签

                    string allAttr = "";
                    
                    foreach (string attr in attrs)
                    {
                       
                        Dictionary<string, string> attrlist = tu.Getattribute(attr);
                        string attrvalue = "";
                        int filedID = 0;
                        string field = attrlist["field"].Replace("\"", "").ToLower();
                        int length = 0;
                        //循环累加的连接字符串
                      
                        foreach (ArtBase ab in ablist)
                        {
                            if (field == "title")
                            {
                                attrvalue = ab.title.ToString();
                            }
                            else if (field == "id")
                            {
                                attrvalue = ab.id.ToString();
                            }
                            else if (field == "adddate")
                            {
                                attrvalue = ab.addDate.ToString();
                            }
                            else if (field == "clickcount")
                            {
                                attrvalue = ab.clickCount.ToString();
                            }
                            else
                            {
                                //当是属性编号时，将属性编号变成整形，否则报错
                                try
                                {
                                    filedID = int.Parse(field);
                                }
                                catch
                                {
                                    throw new Exception("属性值错误，请检查属性值：" + field);
                                }
                            }
                            //如果有长度要求，那么进行截取
                            if (!string.IsNullOrEmpty(attrlist["length"]))
                            {
                                string l = attrlist["length"].Replace("\"", "");
                                if (!string.IsNullOrEmpty(l))
                                {
                                     length = Convert.ToInt32(l);
                                    attrvalue = StringUtil.GetText(attrvalue, length);
                                }
                            }

                            //将link标签中所有属性数据替换掉
                            allAttr += innerHTML.Replace(attr, attrvalue);

                        }
                        //文章循环完毕，替换调<cms:links>标记
                        tag.Replace(str, allAttr);
                    }
                   
                }

                

            }
            return ctx;
        }



    }
}